package com.fast.security;

import com.fast.enums.CodeEnum;
import com.fast.pojo.AjaxResult;
import com.fast.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.security.AuthenticationEntryPointImpl
 * @description: TODO
 * @since 2024/6/30 14:59
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    /**
     * ExceptipnTranslationFilter出现AuthenticationException(未登录导致)
     * 或authentication为匿名/记住我时出现的AccessDeniedException(未设置authentication导致)
     * 时会回调commence方法
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        AjaxResult UNAUTHORIZED = AjaxResult.error(CodeEnum.UNAUTHORIZED.getCode(), CodeEnum.UNAUTHORIZED.getMsg());
        ResponseUtil.responseData(response, UNAUTHORIZED, MediaType.APPLICATION_JSON_VALUE);
    }
}
